﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using Flagwind.Services;
using Flagwind.Resources;

namespace Flagwind.Options.Commands
{
	/// <summary>
	/// 该命令名为“get”，本命令获取当前选项提供程序中的指定选项路径的配置信息。
	/// </summary>
	/// <remarks>
	///		<para>该命令的用法如下：</para>
	///		<code>[options.]get path1 path2 ... path#n</code>
	///		<para>通过 arguments 来指定要查找的选项路径。</para>
	/// </remarks>
	[DisplayName("${Text.OptionsGetCommand.DisplayName}")]
	[Description("${Text.OptionsGetCommand.Description}")]
	public class OptionsGetCommand : CommandBase<CommandContext>
	{
		#region 构造方法

		public OptionsGetCommand() : base("Get")
		{
		}

		public OptionsGetCommand(string name) : base(name)
		{
		}

		#endregion

		#region 重写方法

		protected override object OnExecute(CommandContext context)
		{
			var optionProvider = OptionsCommand.GetOptionProvider(context.CommandNode);

			if(optionProvider == null)
			{
				throw new CommandException(ResourceUtility.GetString("Text.CannotObtainCommandTarget", "OptionProvider"));
			}

			object result = null;

			if(context.Expression.Arguments.Length == 0)
			{
				throw new CommandException(ResourceUtility.GetString("Text.Command.MissingArguments"));
			}

			if(context.Expression.Arguments.Length == 1)
			{
				result = optionProvider.GetOptionValue(context.Expression.Arguments[0]);
			}
			else
			{
				result = new object[context.Expression.Arguments.Length];

				for(int i = 0; i < context.Expression.Arguments.Length; i++)
				{
					((object[])result)[i] = optionProvider.GetOptionValue(context.Expression.Arguments[i]);
				}
			}

			//打印获取的结果信息
			context.Output.WriteLine(Flagwind.Runtime.Serialization.Serializer.Text.Serialize(result));

			return result;
		}

		#endregion
	}
}